#!/usr/bin/env python
#coding:utf-8

import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade

class MyWindow:
    def __init__(self):
        self.wTree = gtk.glade.XML('my1.glade')
        self.window = self.wTree.get_widget('MainWindow')
        dic = {'OnQuit'              :  self.OnQuit,
               'OnAbout'             :  self.OnAbout,
               'OnMainWindowDestroy' :  gtk.main_quit,
               'OnOpenFile'          :  self.OnOpenFile,
               'OnFont'              :  self.OnFont,
               'OnColor'             :  self.OnColor
               }                
        self.wTree.signal_autoconnect(dic)
        self.window.connect('destroy', lambda q:gtk.main_quit())
        self.window.show()

    def OnQuit(self, widget):
        gtk.main_quit()

    def OnOpenFile(self, widget):
        dialog = gtk.FileChooserDialog('Open',
                                       None,
                                       gtk.FILE_CHOOSER_ACTION_OPEN,
                                       (gtk.STOCK_CANCEL,
                                        gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_OPEN,
                                        gtk.RESPONSE_OK))
        f = gtk.FileFilter()
        f.set_name('ALL Files')
        f.add_pattern('*')
        dialog.add_filter(f)

        f = gtk.FileFilter()
        f.set_name('Python')
        f.add_pattern('*.py')
        f.add_pattern('*.pyw')
        dialog.add_filter(f)

        r = dialog.run()
        if r == gtk.RESPONSE_OK:
            print dialog.get_filename()

        dialog.destroy()

    def OnFont(self, widget):
        fontdlg = gtk.FontSelectionDialog('Choose Font')
        r = fontdlg.run()
        if r == gtk.RESPONSE_OK:
            print fontdlg.get_font_name()
        fontdlg.destroy()

    def OnColor(self, widget):
        dlg = gtk.ColorSelectionDialog('Choose Color')
        dlg.colorsel.set_has_palette(True)
        r = dlg.run()
        if r == gtk.RESPONSE_OK:
            print dlg.colorsel.get_current_color()

        dlg.destroy()

    def OnAbout(self, widget):
        msg = gtk.MessageDialog(self.window,
                                gtk.DIALOG_MODAL,
                                gtk.MESSAGE_INFO,
                                gtk.BUTTONS_OK,
                                'Demo by WyattWang')
        msg.run()
        msg.destroy()

    def main(self):
        gtk.main()

if __name__ == '__main__':
    window = MyWindow()
    window.main()
